﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Chaos
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            view.manager m = FindResource("view_manager") as view.manager;
            if (m != null)
            {
                m.Controls.Add("main_window", this);
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Properties.Settings.Default.UserName = Environment.UserName;    
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            Properties.Settings.Default.Save();
            network.gameclient gc = FindResource("game_client") as network.gameclient;
            if (gc != null)
            {
                gc.shutdown();
            }

            network.gameserver gs = FindResource("game_server") as network.gameserver;
            if (gs != null)
            {
                gs.shutdown();
            }

            network.detectionclient dc = System.Windows.Application.Current.FindResource("detection_client") as network.detectionclient;
            if (dc != null)
            {
                dc.shutdown();
            }

            network.detectionserver ds = FindResource("detection_server") as network.detectionserver;
            if (ds != null)
            {
                ds.shutdown();
            }

        }

        private void Window_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.G )
            {
                if (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.LeftCtrl))
                {
                    if (MainMenu.Visibility == System.Windows.Visibility.Collapsed)
                    {
                        DebugControls.Visibility = System.Windows.Visibility.Visible;
                        MainMenu.Visibility = System.Windows.Visibility.Visible;
                    }
                    else
                    {
                        DebugControls.Visibility = System.Windows.Visibility.Collapsed;
                        MainMenu.Visibility = System.Windows.Visibility.Collapsed;
                    }
                }
            }
        }

        private void Open_Click(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
            dlg.DefaultExt = ".xml";
            dlg.Filter = "Text documents (.xml)|*.xml";
            if (dlg.ShowDialog() == true)
            {
                load(dlg.FileName);
            } 
        }

        private void Save_Click(object sender, RoutedEventArgs e)
        {
            if (Properties.Settings.Default.SaveFile != null)
            {
                save(Properties.Settings.Default.SaveFile);
            }
        }

        private void SaveAs_Click(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
            dlg.DefaultExt = ".xml";
            dlg.Filter = "Text documents (.xml)|*.xml";
            if (dlg.ShowDialog() == true)
            {
                Properties.Settings.Default.SaveFile = dlg.FileName;
                save(Properties.Settings.Default.SaveFile);
            } 
        }

        private void save(String in_FileName)
        {
            model.game g = Application.Current.FindResource("model_game") as model.game;
            stream.ostream_xml s = new stream.ostream_xml();
            s.open(in_FileName);
            g.serialize(s);
            s.close();
        }

        private void load(String in_FileName)
        {
            model.game g = Application.Current.FindResource("model_game") as model.game;
            model.factory f = new model.factory();
            stream.istream_xml s = new stream.istream_xml(f);
            s.open(in_FileName);
            g.serialize(s);
            s.close();
        }

        public void ScreenGrid_show(ContentControl c)
        {
            for (int i = 0; i < ScreenGrid.Children.Count; i++)
            {
                if (ScreenGrid.Children[i] != c)
                {
                    ScreenGrid.Children[i].Visibility = Visibility.Hidden;
                }
            }
            c.Visibility = Visibility.Visible;
        }

        private void StartScreen_OnStart(object sender, view.start.start.OnStartEvent e)
        {
            ScreenGrid_show(ConnectScreen);
            //NewGameScreen.start_network_detection();
        }
    }
}
